package com.example.administrator.leargit.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.example.administrator.leargit.BuildConfig;
import com.example.administrator.leargit.R;
import com.example.administrator.leargit.fragment.Basefragment;

import java.util.Stack;

/**
 * Created by Administrator on 2016/12/22 0022.
 */
public class CommonAvtivity extends BaseActivity {

    private Stack<Basefragment> stack = new Stack<>();
    private static final Handler HANDLER = new Handler();
    private static final int DELAY=150;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIntent().putExtras(savedInstanceState == null ? new Bundle() : savedInstanceState);
        setContentView(R.layout.activity_main);
        addFragment(getIntent());
    }

    private class HideRunable implements Runnable {

        private Basefragment basefragment;

        public HideRunable(Basefragment basefragment) {
            this.basefragment = basefragment;
        }

        @Override
        public void run() {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            for (int i = 0; i < stack.size(); i++) {
                Fragment ft = stack.get(i);
                if (ft != basefragment) {
                    if (ft.isHidden()) {
                        transaction.hide(ft);
                    } else {
                        if (ft.isHidden()) {
                            transaction.show(ft);
                        }
                    }
                }
            }
            try {
                transaction.commit();
            } catch (Exception e) {
                if (BuildConfig.DEBUG) {
                    e.printStackTrace();
                }
            }

        }
    }
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        addFragment(intent);
    }

    @Override
    protected Basefragment addFragment(int id, Basefragment fragment, boolean isAnim) {
        if (id == 0) {

        }
        fragment = super.addFragment(id, fragment, isAnim);
        stack.push(fragment);
        HANDLER.postDelayed(new HideRunable(fragment),DELAY);
        return super.addFragment(id, fragment, isAnim);

    }
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onBackPressed() {
        if (stack.peek().onBackPressed()){
            return;
        }
        if (stack.size() > 1) {
            stack.pop().onHiddenChanged(true);
        } else {
            if (!stack.isEmpty()) {
                stack.peek().onHiddenChanged(true);
            }
            finish();
            return;
        }
        HANDLER.postDelayed(new HideRunable(stack.peek()), DELAY);
        super.onBackPressed();
        onDestroyFragment();
    }

    private void onDestroyFragment() {
        if (stack.size()>0){
            Basefragment bf = stack.peek();
            String title = bf.getTitle();
            setTitle(title);
            bf.onInitActionBar(this);
        }
    }
    public Basefragment getTopFragment(){
        return stack.peek();
    }
}
